import requestUrl from '../../utils/requestUtil.js'


Page({

  /**
   * 页面的初始数据
   */
  data: {
    //种植品种
    plantingType: [],
    list: {
      "avgAge": '',
      "forecastOutputSum": '',
      "lastYearOutputSum": '',
      "maturationTime": '',
      "saleChannel": ' ',
      "produceDataSum": '',
      "skuNo": '',
      "isPromiseNoForbidden": '103101',
      "totleArea": ''
    },
    show: false,
    minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    type: 1,

  },
  onLoad: function (options) {

    if (options.hasOwnProperty('type')) {
      this.setData({
        type: parseInt(options.type)
      })
    }

  },
  onShow: function () {
    let userToken = wx.getStorageSync('token')
    requestUrl.requestUrl({
      url: "/app/ph/prodType",
      method: "get",
      header: {
        token: userToken
      }
    }).then((data) => {
      let list = data.data.list;
      let arr = [];
      list.forEach(element => {
        let Obj = {
          text: element.prodName,
          value: element.prodNo
        }
        arr.push(Obj)
      });
      this.setData({
        plantingType: arr,
        [`list.skuNo`]: arr[0].value,
      })
    }).catch((errorMsg) => {

    })
  },

  onChange(event) {
    this.setData({
      [`list.isPromiseNoForbidden`]: event.detail,
    });
  },
  //提交
  commit() {
    let userToken = wx.getStorageSync('token')
    let data = this.data.list;
    let type = this.data.type;

    for (let i in data) {
      if (data[i] == '') {
        return wx.showToast({
          title: '产品信息未填写完整！',
          icon: 'none',
        })
      }
    }

    requestUrl.requestUrl({
      url: "/app/ph/crops/save",
      params: data,
      header: {
        token: userToken
      }
    }).then((data) => {
      if (data.data.code == 0) {

        wx.navigateBack({
          delta: type
        })
      }
    }).catch((errorMsg) => {

    })

  },


  onClose() {
    this.setData({
      show: false
    });
  },
  //显示
  showPopup() {
    this.setData({
      show: true
    });
  },

  mathChangeDate(times) {
    var date = new Date(times);
    var year = date.getFullYear(); //年份
    var month = date.getMonth() + 1; //月份
    var day = date.getDate(); //日
    return year + "年" + month + "月" + day + "日";
  },
  //选择日期
  onConfirm(e) {

    this.setData({
      show: false,
      [`list.maturationTime`]: this.mathChangeDate(e.detail)
    })
  },
  onCancel() {
    this.setData({
      show: false,

    })
  },

  //选择种植品种
  changeZhi(e) {
    this.setData({
      [`list.skuNo`]: e.detail,
    })
  },
  onChangeavgAge(e) {
    this.setData({
      [`list.avgAge`]: e.detail
    })
  },
  onChangetotleArea(e) {
    this.setData({
      [`list.totleArea`]: e.detail
    })
  },
  onChangeforecastOutputSum(e) {
    this.setData({
      [`list.forecastOutputSum`]: e.detail
    })
  },
  onChangelastYearOutputSum(e) {
    this.setData({
      [`list.lastYearOutputSum`]: e.detail
    })
  },
  onChangeproduceDataSum(e) {
    this.setData({
      [`list.produceDataSum`]: e.detail
    })
  }




})